<?php
namespace Club\Controller;
use Think\Controller;
class VideoController extends PublicController {
    
	 public function videolist(){
        $coach = M('coach');
        $video=$coach->join('video ON video.coachid = coach.coachid')->select();
        $this->assign('video',$video);
        $this->display();
    }

    public function addvideo(){
        $coach = M('coach');
        $data['club']=$_SESSION['club'];
        $coach = $coach->where($data)->select();
        $this->assign('coach',$coach);
        $this->display();
    }

    public function add(){
        $video = M('video');  
        if($video->create()){
            if ($_FILES['video']['error'] == 0 ||$_FILES["pic"]["error"]==0) {
                $info = $this->upload();
                $videoname=$info['video']['savename'];
                $picname=$info['pic']['savename'];
            } else {
                $videoname = "2.mp4";
            }
            $video->video=$videoname;
            $video->time=date('Y-m-d H:i:s');
            $video->pic=$picname;
            if (false !== $video->add()) {
                $this->success('添加成功！', '');
            } else {         
                $this->error('数据错误','');           
            }
        }else {            
            $this->error($video->getError());  
        }
    }

    public function upload(){
        $upload = new \Think\Upload();
        $upload->maxSize = 314572800000000;
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg',"mp4","wmv");
        $upload->rootPath = './Public/photo/';
        $upload->autoSub=false;
        $info = $upload->upload();

        if(!$info) {        
            $this->error($upload->getError());    
        }else{        
           return $info; 
        }                    
    }

    public function delete(){
        $id=$_GET['videoid'];
        $video=M('video');
        // $pic=$video->where("videoid=$id")->find();
        // unlink(realpath("./Public/photo/".$pic["pic"]));
        $video=$video->where("videoid=$id")->delete();
        if($video=1){
            $this -> success('删除成功！','');
        }else{
            $this->error('删除失败！','');
        }
    }
    /*获取视频名 开发人：周弈男*/
    public function videoname(){
        $id=$_GET['id'];
        $video=M('video');
        $video=$video->where("videoid=$id")->find();
        $this->ajaxReturn($video);
    }
}